package com.xit.hitmouse.service;

import java.util.HashMap;
import java.util.Map;

//import com.example.hitmouse.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;

import androidx.annotation.Nullable;

import com.xit.hitmouse.R;

public class MusicService {
			MediaPlayer player;
			SoundPool pool;
			Context context;
			Map<Integer,Integer> soundMap;
			public MusicService(Context context,int resId) {
				super();
				this.context=context;

				initMedia(resId);
				initSound();
			}
			//游戏进入时的背景音乐
			public MusicService(Context context) {
				super();
				this.context=context;
				initMedia(R.raw.background_sound2);
				initSound();
			}
			private void initSound() {
				
				pool = new SoundPool(3, AudioManager.STREAM_MUSIC, 5);
				soundMap = new HashMap<Integer, Integer>();
				soundMap.put(R.raw.hit_sound, pool.load(context,R.raw.hit_sound,1));
				soundMap.put(R.raw.dizzy, pool.load(context,R.raw.dizzy,1));
				soundMap.put(R.raw.laugh, pool.load(context,R.raw.laugh,1));
				
			}
			private void initMedia( int resId) {
//				player = MediaPlayer.create(context, R.raw.background_sound);
				player = MediaPlayer.create(context, resId);

				player.setLooping(true);
				
				
			}
			//播放打击时候的音乐
			public void playSound(int resId){
				Integer soundId =soundMap.get(resId);
				if(soundId!=null){
					pool.play(soundId, 1, 1, 1, 0, 1);
				}
				
			}
			//播放背景音乐
			public void playBackground(){
				player.start();
			}

			public void stop(){
				if(player.isPlaying()){
					player.stop();
				}
			}
			public void pause(){
				if(player.isPlaying()){
					player.pause();
				}
			}

			public void continuePlay() {  
		        player.start();  
		    }  

			public void close(){
				if(player==null){
					return;
				}
				if(player.isPlaying()){
					player.stop();
				}
			}

			public Boolean isNowPlay(){  
		        if(player.isPlaying()){  
		            return true;  
		        }  
		        return false;  
		    }  
}
